package com.weitu.emergency.service.impl;

import com.weitu.emergency.entity.TeamAndUser;
import com.weitu.emergency.mapper.TeamAndUserMapper;
import com.weitu.emergency.service.TeamAndUserService;
import com.weitu.permission.user.entity.User;
import com.weitu.permission.user.entity.UserDepartment;
import com.weitu.permission.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangqingxi 2017/5/23
 */
@Service
public class TeamAndUserServiceImpl implements TeamAndUserService {

    private final TeamAndUserMapper teamAndUserMapper;
    private final UserMapper userMapper;

    @Autowired
    public TeamAndUserServiceImpl(TeamAndUserMapper teamAndUserMapper, UserMapper userMapper) {
        this.teamAndUserMapper = teamAndUserMapper;
        this.userMapper = userMapper;
    }

    @Override
    public List<TeamAndUser> findByTeamId(String emergencyTeamId, String userType) {
        List<TeamAndUser> teamAndUserList=teamAndUserMapper.findByTeamId(emergencyTeamId,userType);
        User user;
        for(TeamAndUser teamAndUser:teamAndUserList){
            String userId=teamAndUser.getUserId();
            user=userMapper.selectById(userId);
            if(null != user){
                teamAndUser.setPostName(user.getPostName());
                teamAndUser.setUserName(user.getRealName());
                StringBuffer sb= new StringBuffer();
                List<UserDepartment> userDepartment=user.getUserDepartmentList();
                for(int i=0;i<userDepartment.size();i++){
                    sb.append(userDepartment.get(i).getDeptName());
                    if (i<userDepartment.size()-1){
                        sb.append(",");
                    }
                }
                teamAndUser.setDeptName(sb.toString());
            }
        }
        return teamAndUserList;
    }
}
